package com.zm.devmag.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zm.devmag.back.vo.Admin;
import com.zm.devmag.util.IpUtil;



public class BackFilter implements Filter {

	/*private String encode;*/
	private Logger logger = LoggerFactory.getLogger(BackFilter.class);
	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		String uri = req.getRequestURI();
	
		if(!StringUtils.isEmpty(uri) && uri.contains("back")){
			
			HttpSession session  = req.getSession();
			Admin admin = (Admin) session.getAttribute("admin");
			System.out.println(uri);
			if(admin != null){
				chain.doFilter(request, response);
				logger.info("[已登陆]uri-->" + uri + " ip-->" + IpUtil.getIp(req)+ "param-->" +req.getQueryString());
			}else{
				logger.info("[未登陆] uri-->" + uri + " ip-->" + IpUtil.getIp(req));
				response.setContentType("text/html;charset=UTF-8");
		        response.getWriter().write(new String("您还未登陆，3秒后跳到登陆页面"));
				response.getWriter().write("<script>setTimeout(function(){location.href='/loginUi.do'},1500);</script>");
			}
		}else{
			chain.doFilter(request, response);
		}
		
		
	}
	
	@Override
	public void init(FilterConfig config) throws ServletException {

	}
	


}
